Rohit Singh's profile

Solidity smart contract example




Solidity smart contract example



Many excellent Solidity smart contract examples are offered, but the ERC-20 token standard stands out as noteworthy many excellent Solidity smart contract examples offered, but the ERC-20 token standard stands out as a noteworthy one.

Tokens on the Ethereum blockchain must adhere to a set of rules and regulations called the ERC-20 standard. It establishes a standard interface for tokens, making it simpler for developers to design new tokens that work with already-released software and digital wallets.


A prime example ERC-20 token contract is shown here:
pragma solidity ^0.8.0;
contract MyToken {
    string public name = "My Token";
    string public symbol = "MTK";
    uint256 public totalSupply = 1000000;

    mapping(address => uint256) balances;

    constructor() {
        balances[msg.sender] = totalSupply;
    }

    function balanceOf(address account) public view returns (uint256) {
        return balances[account];
    }

    function transfer(address recipient, uint256 amount) public returns (bool) {
        require(amount <= balances[msg.sender], "Insufficient balance");
        balances[msg.sender] -= amount;
        balances[recipient] += amount;
        emit Transfer(msg.sender, recipient, amount);
        return true;
    }

    event Transfer(address indexed from, address indexed to, uint256 value);
}

We define a token named "My Token" with the symbol "MTK" and a 1,000,000 supply in total in this contract. Users can transfer tokens to other addresses using the transfer function, and the balances mapping keeps up track of each address's token balance.

The emit statement creates an event to alert the other agreements about the transfer, and the need statement determines if the sender has sufficient balance to complete the transfer.

This agreement is an easy illustration of an ER.
Solidity smart contract example
Published:

Solidity smart contract example

Published:

Creative Fields